﻿using Microsoft.AspNetCore.Mvc;
using Simple.Tools;
using Simple.Tools.Extensions;
using SimpleSaas.Common;
using SimpleSaas.Common.Execption;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using BaseMessage = SimpleSaas.Common.BaseMessage;

namespace SimpleSaas.Web.Common
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class BaseController : ControllerBase
	{
		public BaseController()
		{
			//SetLoginUserInfo();
		}
		#region 200
		public BaseMessage Ok200()
		{
			return new BaseMessage("成功!", 200);
		}
		public BaseMessage Ok200(string message)
		{
			return new BaseMessage(message, 200);
		}

		public BaseMessage Ok200<TData>(List<TData> data, string message)
		{
			return new SimpleSaas.Common.PageMessage<TData>(data, message, 200);
		}

		public BaseMessage Ok200<TData>(List<TData> data)
		{
			return new SimpleSaas.Common.PageMessage<TData>(data, 200);
		}

		public BaseMessage Ok(object data)
		{
			return new SimpleMessage(data);
		}
		public BaseMessage ReturnOk200(object data, string message)
		{
			return new SimpleMessage(data, message, 200);
		}

		#endregion

		#region 400
		public BaseMessage Bad400(string message)
		{
			return new BaseMessage(message, 400);
		}

		public BaseMessage ReturnBad400(object data, string message)
		{
			return new SimpleMessage(data, message, 400);
		}
		#endregion

		#region 500
		public BaseMessage Bad500(string message)
		{
			return new BaseMessage(message, 500,false);
		}
		public BaseMessage ReturnBad500(object obj, string message)
		{
			return new SimpleMessage(obj, message, 500,false);
		}
		/// <summary>
		/// 
		/// </summary>
		public void SetLoginUserInfo()
		{
			if (SimpleCheck.IsNotNullOrEmpty(User.Claims))
			{
				App.UserInfo.Value = new LoginUserInfo();
				App.UserInfo.Value.LoginFlag = true;
				App.UserInfo.Value.UserId = GetUserId();
				App.UserInfo.Value.UserName = GetUserName();
				App.UserInfo.Value.TenantName = GetTenantName();
				App.UserInfo.Value.TenantId = GetTenantId();
				App.UserInfo.Value.DatabaseId = GetDatabaseId();
			}
		}
		/// <summary>
		/// 获取用户id
		/// </summary>
		/// <returns></returns>
		public long GetUserId()
		{
			Claim claim = GetClaim(ClaimTypes.NameIdentifier);
			return claim.Value.ToInt64();
		}
		/// <summary>
		/// 获取用户名称
		/// </summary>
		/// <returns></returns>
		public string GetUserName()
		{
			Claim claim = GetClaim(ClaimTypes.Name);
			return claim.Value.ToString();
		}

		/// <summary>
		/// 获取用户名称
		/// </summary>
		/// <returns></returns>
		public long GetTenantId()
		{
			Claim claim = GetClaim("tenantId");
			return claim.Value.ToInt64();
		}
		/// <summary>
		/// 获取用户名称
		/// </summary>
		/// <returns></returns>
		public string GetTenantName()
		{
			//
			Claim claim = GetClaim("tenantName");
			return claim.Value.ToString();
		}

		/// <summary>
		/// 获取用户名称
		/// </summary>
		/// <returns></returns>
		public long GetDatabaseId()
		{
			Claim claim = GetClaim("databaseId");
			return claim.Value.ToInt64();
		}
		public Claim GetClaim(string claimName)
		{
			Claim claim = User.Claims.FirstOrDefault(c => c.Type.Equals(claimName));
			if (SimpleCheck.IsNull(claim))
			{
				throw new BusinessException("请重新登录!");
			}
			return claim;
		}
		#endregion
	}
}